package com.intellij.ide.license.impl;

import com.intellij.ide.license.LicenseValidator;
import com.intellij.licensecommon.data.LicenseData;
import com.intellij.licensecommon.data.SubscriptionLicenseDataImpl;

import java.util.Date;

/*
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.updateSettings.impl.UpdateChecker;
*/

public abstract class GeneralLicenseValidator implements LicenseValidator {
    private final int licenseType;

    protected GeneralLicenseValidator(int licenseType) {
        this.licenseType = licenseType;
    }

    @Override
    public boolean isValid(LicenseData licenseData) throws LicenseException {
        if (licenseData == null) {
            return false;
        }

        checkValid(licenseData);
        checkHacked(licenseData);
        checkMatchesProduct(licenseData);
        checkLicenseExpired(licenseData);
        checkEvaluationExpired(licenseData);

        if (b(licenseData)) {
            checkUpgrade(licenseData);
        }
        return true;
    }

    private boolean b(LicenseData licenseData) {
        if ((licenseData.willExpire()) && ((licenseData.isOpenSource()) || (licenseData.isClassroom()) || ((licenseData instanceof EvaluationLicense)))) {
            return false;
        }
        return true;
    }

    protected static void checkHacked(LicenseData licenseData) {
        if (licenseData == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"0", "com/intellij/ide/license/impl/GeneralLicenseValidator", "checkHacked"}));
        }
        SubscriptionLicenseDataImpl.i = licenseData.k();
        if (SubscriptionLicenseDataImpl.i) {
            // TODO
            // UpdateChecker.addUpdateRequestParameter("version", ApplicationInfoImpl.getShadowInstance().getMajorVersion());
        }
    }

    protected void checkEvaluationExpired(LicenseData licenseData) throws LicenseException {
        if (((licenseData instanceof EvaluationLicense)) && (licenseData.isEvaluationExpired(new Date()))) {
            throw new EvaluationLicenseExpiredException();
        }
    }

    private void checkLicenseExpired(LicenseData licenseData) throws LicenseException {
        if (licenseData == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"0", "com/intellij/ide/license/impl/GeneralLicenseValidator", "checkLicenseExpired"}));
        }
        if ((licenseData instanceof EvaluationLicense)) {
            return;
        }
        if (!licenseData.willExpire()) {
            return;
        }
        if (licenseData.isEvaluationExpired(new Date())) {
            throw new ExpiredLicenseException();
        }
    }

    protected void checkMatchesProduct(LicenseData licenseData) throws LicenseException {
        if (licenseData == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"0", "com/intellij/ide/license/impl/GeneralLicenseValidator", "checkMatchesProduct"}));
        }
        if (licenseData.getLicenseType() != this.licenseType) {
            throw new InvalidLicenseException();
        }
    }

    protected abstract void checkUpgrade(LicenseData licenseData) throws LicenseException;

    protected void checkValid(LicenseData licenseData) throws LicenseException {
        if (licenseData == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"0", "com/intellij/ide/license/impl/GeneralLicenseValidator", "checkValid"}));
        }
        if (licenseData.isValid()) {
            return;
        }
        if (licenseData.isFromUser()) {
            throw new InvalidLicenseException();
        }
        throw new LicenseCorruptException();
    }
}
